package ic.doc.co2nudge.client.widgets;

import ic.doc.co2nudge.client.utils.UserService;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MainPage extends Composite {

	@UiTemplate("MainPage.ui.xml")
	interface MainPageUiBinder extends UiBinder<Widget, MainPage>{}
	private static MainPageUiBinder uiBinder = GWT
		.create(MainPageUiBinder.class);
	
	@UiField DockLayoutPanel	pnlMain;
	@UiField DeckPanel 			pnlControls;
	
	@UiField Label 				lblLogoLine;
	@UiField Label 				lblWelcomeLine;
	@UiField Label 				lblUserName;
	
	@UiField NewTripBuildPanel 	pnlNewTripBuildPanel;
	@UiField PredictionPanel 	pnlPrediction;
	@UiField StatisticsPanel 	pnlStatistics;
	@UiField ControlPanel		pnlGoalSetUp;
	
	@UiField Button 			btnNewTrip;
	@UiField Button 			btnPredictionPanel;
	@UiField Button 			btnStatisticsPanel;
	@UiField Button 			btnGoalSetUpPanel;
	@UiField Button 			btnLogInOut;
	
	public MainPage() {
		initWidget(uiBinder.createAndBindUi(this));
		buildUI();
	}
	
	private void buildUI(){
		
		setUpHandlers();
		pnlControls.showWidget(1);
		lblUserName.setText(UserService.getUserService()
				.getCurrentUser().getName());
	}
	
	private void setUpHandlers(){
		
		ClickHandler btnClickHandler=new ClickHandler(){

			int pnlIndex=0;
			
			@Override
			public void onClick(ClickEvent event) {
				
				Object src=event.getSource();
				if (src.equals(btnNewTrip)){
					pnlIndex=0;
				}
				if (src.equals(btnStatisticsPanel)){
					
					pnlIndex=1;	
				}
				if (src.equals(btnPredictionPanel)){
					pnlIndex=2;	
				}
				if (src.equals(btnGoalSetUpPanel)){
					pnlIndex=3;
				}
				
				pnlControls.showWidget(pnlIndex);
			}
		};
		
		btnNewTrip.addClickHandler( btnClickHandler);
		btnPredictionPanel.addClickHandler( btnClickHandler);
		btnGoalSetUpPanel.addClickHandler( btnClickHandler);
		btnStatisticsPanel.addClickHandler( btnClickHandler);
	}
}